<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if ( ! function_exists('trim_string'))
{
 	
	function trim_string($value,$cut_point,$suffix = "")
	{
	                                                         
        if($value==NULL || $value == FALSE)
            return $value;
        if(trim($value)=="")
            return "";

        if(strlen($value)<=$cut_point)
            return $value;
        
        $final_value = substr($value,0,$cut_point-strlen($suffix));
        $last_space_pos = strripos($final_value," ");
        if($last_space_pos === FALSE)
            return $final_value . $suffix;

        return substr($final_value,0,$last_space_pos) . $suffix; 
	
	}
}

/*
if ( ! function_exists('highlight_word'))
{
    function highlight_word($str, $word, $tag_open = '<strong>', $tag_close = '</strong>')
    {
        if ($str == '')
        {
            return '';
        }
    
        if ($phrase != '')
        {
            return preg_replace('/('.preg_quote($phrase, '/').')/i', $tag_open."\\1".$tag_close, $str);
        }

        return $str;
    }
}
*/



if ( ! function_exists('highlight'))
{
    function highlight($search="", $string="", $trim_size=FALSE, $prefix = '<span class="highlight">', $suffix = '</span>')
    {    
        $pos = stripos($string, $search); // returns position of first occurence of $search in $string

        if ($pos === false) { 
            if ($trim_size === FALSE) return $string;
            else return (trim_string($string, $trim_size, '...'));
        } else {

            if (($trim_size !== FALSE) && (strlen($string) > $trim_size)){ //if trim size is set and string is longer than that
                $start = $pos-$trim_size/2; // set start point at $trim_size/2 character before the first occurence
                $end = $pos+strlen($search)+$trim_size/2; // set end point at $trim_size/2 character after the first occurence

                if($start<0) {
                    if ($end > strlen($string)+$start ) $end = strlen($string);
                    else $end -= $start;
                    $start = 0;
                }                                                                

                if ($end>strlen($string)) {

                    if ($start < $end-strlen($string) ) $start = 0;
                    else $start -= $end-strlen($string);
                    $end = strlen($string);
				}                                                                

                if ($start>0){ 
                    
                    $start = stripos($string, " ", $start)+1;
					$end = $pos+strlen($search)+$trim_size/2;
                    $string = "...".substr($string, $start); //cut string at start and return second part
                    $pos -= $start-3;// adjust $pos
                    //$string = "...".$string;     
                    //$pos += 3;				
                }
                $string = trim_string($string, $end-$start, '...');

            }

            $pos_array = array($pos); //array of positions where $search starts
			
			while ($pos = stripos($string, $search, $pos+strlen($search))) {
                $pos_array[] = $pos;
            }

            //loop through positions and insert $prefix and $sufix. First we flip the array backwards because positions will change
            foreach(array_reverse($pos_array) as $pos){
                
                $string = substr_replace($string, $suffix, $pos+strlen($search), 0);
                $string = substr_replace($string, $prefix, $pos, 0);

            }

            return $string;

        }
    }
}

if ( ! function_exists('trim_highlight'))
{
    function trim_highlight($search="", $string="", $size = 100)
    {    

        //trim string to $size around $search

        // highlight remaining string

        $pos = stripos($string, $search); // returns position of first occurence of $search in $string
        if ($pos === false) return $string;
        else {
            $pos_array = array($pos); //array of positions where $search starts
            while ($pos = stripos($string, $search, $pos+strlen($search))) {
                $pos_array[] = $pos;
            }

            //loop through positions and insert $prefix and $sufix. First we flip the array backwards because positions will change
            foreach(array_reverse($pos_array) as $pos){
                
                $string = substr_replace($string, $suffix, $pos+strlen($search), 0);
                $string = substr_replace($string, $prefix, $pos, 0);

            }
            return $string;

        }
    }
}
    
/**
* returns a string with the highlighted words of $search on $source.
* HTML is added to make the highlight.
* 
* @access protected 
* @param source 
* @param search 
* @return string 
*/

if ( ! function_exists('highlight_text'))
{
    function highlight_text($source, $search)
    {
        $source_words = explode(' ',$source);
        $search_words = explode(' ',$search);
        
        uasort($search_words, "str_len_cmp");
        
        foreach($source_words as $pos => $sw)
        {
            foreach($search_words as $pos_s => $srch_w)
            {
                if(strlen($srch_w) < 2){
                    unset($search_words[$pos_s]);
                    continue;
                }
                $word_start = stripos($sw, $srch_w);
                if($word_start !== FALSE)
                {
                    $word = substr($source_words[$pos], $word_start, strlen($srch_w));
                    $rest_left = substr($source_words[$pos],0, $word_start);
                    $rest_right = substr($source_words[$pos], $word_start+strlen($srch_w));
                    //echo "word: ".$word."|<br />";
                    //echo "rest: ".$rest."|<br />";
                    $source_words[$pos] = $rest_left.'<span style="background-color: yellow;">'.$word.'</span>'.$rest_right;
                    
                    break;
                }
            }
        }
        return implode(" ", $source_words);
    }
}

                                                       


/**
* Used only in highlight text to sort the search words by length, so we don't highlight the word on in don't
* (being on and don't search words).
* $str1 and $str2 are the strings to compare.
* 
* @access protected 
* @param str1 
* @param str2 
* @return int'
*/

if ( ! function_exists('str_len_cmp'))
{
    function str_len_cmp($str1, $str2)
    {
        if(strlen($str1) == strlen($str2))
            return 0;
        return strlen($str1) < strlen($str2) ? 1 : -1;
    }
}
/**
* Returns a "string window" centered on the specified position $center and with the size of $window_size.
* If the position is close to the begin or at the begin or close to the end or at the end 
* of the string than it compensates it on the other side of the cut.
* 
* @access protected 
* @param center 
* @param source
* @param window_size 
* @return string 
*/

if ( ! function_exists('string_window'))
{
    function string_window($source, $center, $window_size)
    {
        $offset = (int)$window_size/2;
        
        if($center-$offset>0)
            $pos_left = $center-$offset;
        else
            $pos_left = 0;
        
        if($pos_left+$window_size > strlen($source))
            $pos_right = strlen($source);
        else
            $pos_right = $pos_left+$window_size;
        
        $return = trim(substr($source, $pos_left, $pos_right-$pos_left));
        
        if($pos_left > 0)
            $return = "...".$return;
        
        if($pos_right < strlen($source))
            $return = $return."...";
        
        return $return;
    }
}
/* End of file order_helper.php */
/* Location: ./system/application/helpers/string_helper.php */